package com.ruoyi.web.controller.store;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Vendor;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.VendorService;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ruohao
 * @Date: 2023/8/13
 **/
@RestController
@RequestMapping("/store/vendor")
public class VendorController extends BaseController {
    @Resource
    private VendorService vendorService;

    @Log(title = "供应商导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Vendor vendor)
    {
        List<Vendor> vendors = vendorService.selectList(vendor).stream()
                .filter(v -> "启用".equals(v.getStatus()))
                .collect(Collectors.toList());
        ExcelUtil<Vendor> util = new ExcelUtil<Vendor>(Vendor.class);
        util.exportExcel(response, vendors, "供应商");
    }

    @Log(title = "查询人员管理", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(Vendor vendor)
    {
        startPage();
        List<Vendor> list = vendorService.selectList(vendor);
        return getDataTable(list);
    }

    @Log(title = "查询人员管理All", businessType = BusinessType.OTHER)
    @GetMapping("/listAll")
    public TableDataInfo listAll(Vendor vendor)
    {
        List<Vendor> list = vendorService.selectList(vendor);
        if (CollectionUtils.isEmpty(list)) {
            return getDataTable(list);
        }
        return getDataTable(list.stream()
                .filter(v -> "启用".equals(v.getStatus()))
                .collect(Collectors.toList()));
    }
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(vendorService.selectById(id));
    }

    @Log(title = "新增人员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Vendor vendor)
    {
        vendor.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = vendorService.create(vendor);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "修改人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Vendor vendor) {
        vendor.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        Boolean result = vendorService.update(vendor);
        if (result) {
            return success();
        }
        return error();
    }

    @Log(title = "删除人员管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(vendorService.deleteById(id));
    }


}
